package com.persondemo.videoappliction.ui.history;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.persondemo.videoappliction.R;
import com.persondemo.videoappliction.bean.CollectionBean;
import com.persondemo.videoappliction.component.ApplicationComponent;
import com.persondemo.videoappliction.database.CollectDao;
import com.persondemo.videoappliction.ui.adapter.HistoryAdapter;
import com.persondemo.videoappliction.ui.base.BaseActivity;
import com.persondemo.videoappliction.ui.video.VideoDetilsActivity;
import com.persondemo.videoappliction.ui.vip.PersonSettingActivity;
import com.persondemo.videoappliction.widget.ClearEditText;
import com.persondemo.videoappliction.widget.SpaceItemDecoration;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/4/10.
 */

public class HistoryActivity extends BaseActivity {
    @BindView(R.id.history_recycler)
    RecyclerView historyRecycler;
    List<CollectionBean> list = new ArrayList<>();
    @BindView(R.id.history_back)
    ImageView historyBack;
    @BindView(R.id.delte_text)
    TextView delteText;
    private HistoryAdapter adapter;

    @Override
    public void onRetry() {

    }

    @Override
    public int getContentLayout() {
        return R.layout.module_activity_history;
    }

    @Override
    public void initInjector(ApplicationComponent appComponent) {

    }

    @Override
    public void bindView(View view, Bundle savedInstanceState) {
        historyBack.setOnClickListener(v -> finish());
        delteText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showpop();
            }
        });
    }

    private void showpop() {
        AlertDialog.Builder builder = new AlertDialog.Builder(HistoryActivity.this);
        final ClearEditText nickname_input;
        final AlertDialog dialog;
        View dialgview = LayoutInflater.from(HistoryActivity.this).inflate(R.layout.item_delte, null, false);
        dialog = builder.setView(dialgview).create();
        if (dialog.isShowing()) {
            dialog.dismiss();
        } else {
            dialog.show();
        }
        dialgview.findViewById(R.id.dilog_nickname_reset_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        dialgview.findViewById(R.id.dilog_nickname_reset_comfrim).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (list.size() > 0 && list != null) {
                    CollectDao.delteall();
                    list.clear();
                    adapter.notifyDataSetChanged();
                }
                dialog.dismiss();
            }
        });
    }

    @Override
    public void initData() {
        list = CollectDao.getChannels();
        if (list.size() != 0) {
            Collections.reverse(list);
        }
        if (list.size() != 0) {
            historyRecycler.setLayoutManager(new GridLayoutManager(HistoryActivity.this, 1));
            adapter = new HistoryAdapter(list, HistoryActivity.this);
            adapter.setNewData(list);
            historyRecycler.setAdapter(adapter);
            adapter.setEnableLoadMore(false);
            historyRecycler.addItemDecoration(new SpaceItemDecoration(10));
            historyRecycler.setNestedScrollingEnabled(false);
            adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
            adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                    gotoVideodetils(list.get(position).getVodeoid());
                }
            });
        }

    }

    public void gotoVideodetils(int id) {
        hideLoadingDialog();
        Intent intent = new Intent(HistoryActivity.this, VideoDetilsActivity.class);
        Bundle bundle = new Bundle();
        bundle.putInt("id", id);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
    }
}
